//
//  PayView.m
//  ceshi
//
//  Created by liutaifeng on 16/5/18.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "AddView.h"
#import "Common.h"
const NSTimeInterval AnimationChangeColorDuration = 0.5;
@interface AddView ()
<
 UITextFieldDelegate,
 UICollectionViewDataSource,
 UICollectionViewDelegate,
 BottomCollectionViewCellDelegate
>





@property (nonatomic, strong) UICollectionView *bottomCollectionView;
@property (nonatomic, strong) UIView *keyboardView;

@property (nonatomic, strong) UICollectionView * keyboardCollectionView;
@property (nonatomic, strong) NSArray *keyboardArray;

@property (nonatomic, strong) UIButton *returnButton;
@property (nonatomic, strong) UIButton *certainButton;

@property (nonatomic, strong) NSMutableString *string;

@property (nonatomic, assign) BOOL isBooL;


@property (nonatomic, strong) UIPageControl *page;


@property (nonatomic, strong) UIView *operateView;

@property (nonatomic, strong) UIButton *dateButton;

@property (nonatomic, strong) UIButton *editButton;

@property (nonatomic, strong) NSString *pic;
@property (nonatomic, strong) NSString *describe;


@property (nonatomic, strong) NSMutableArray *picArray;
@property (nonatomic, strong) NSMutableArray *titleArray;
@property (nonatomic ,strong) NSArray *colorArray;

@property (nonatomic, strong) NSMutableArray *inComePicArray;
@property (nonatomic, strong) NSMutableArray *incomeTitleArray;
@property (nonatomic ,strong) NSArray *incomeColorArray;

@property (nonatomic, assign) BOOL isReset;

@end

@implementation AddView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)setIsChange:(BOOL)isChange {
    if (_isChange != isChange) {
        _isChange = isChange;
    }
}
- (void)setIsState:(BOOL)isState {
    if (_isState != isState) {
        _isState = isState;
        if (_isState == 1) {
            _page.numberOfPages = 2;
            if (_isChange == 1) {
                UIColor *bgColor = [UIColor colorWithRed:127.0 / 255.0 green:184.0 / 255.0 blue:14 / 255.0 alpha:1];
                [_topView mdInflateAnimatedFromPoint:CGPointMake(0, _topView.frame.size.height / 2)  backgroundColor:bgColor duration:AnimationChangeColorDuration completion:nil];
                [_picImageView setImage:[UIImage imageNamed:@"yiban.png"]];
                _titleLabel.text = @"一般";
                self.pic = @"yiban.png";

            } else {
                UIColor *bgColor = [UIColor colorWithRed:127.0 / 255.0 green:184.0 / 255.0 blue:14 / 255.0 alpha:1];
                _topView.backgroundColor = bgColor;
                [_picImageView setImage:[UIImage imageNamed:@"yiban.png"]];
                _titleLabel.text = @"一般";
                self.pic = @"yiban.png";

            }
        }
        else {
            _page.numberOfPages = 1;
            if (_isChange == 1) {
                UIColor *bgColor = [UIColor colorWithRed:234.0 / 255.0 green:128.0 / 255.0 blue:16 / 255.0 alpha:1];
                [_picImageView setImage:[UIImage imageNamed:@"yiban1.png"]];
                [_topView mdInflateAnimatedFromPoint:CGPointMake(0, _topView.frame.size.height / 2)  backgroundColor:bgColor duration:AnimationChangeColorDuration completion:nil];
                _titleLabel.text = @"一般";
                self.pic = @"yiban1.png";
            } else {
                UIColor *bgColor = [UIColor colorWithRed:234.0 / 255.0 green:128.0 / 255.0 blue:16 / 255.0 alpha:1];
                _topView.backgroundColor = bgColor;
                [_picImageView setImage:[UIImage imageNamed:@"yiban1.png"]];
                
                _titleLabel.text = @"一般";
                self.pic = @"yiban1.png";
            }
           
        }
        //        _titleLabel.text = @"一般";
        [_bottomCollectionView reloadData];
    }
}

- (void)createView {
    _isState = 1;
    
    self.keyboardArray = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9" , @"清零", @"0", @"."];
    
    
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAIN_WIDTH, MAIN_HEIGHT / 12)];
    [self addSubview:_topView];
    UIColor *bgColor = [UIColor colorWithRed:127.0 / 255.0 green:184.0 / 255.0 blue:14 / 255.0 alpha:1];
    self.topView.backgroundColor = bgColor;
    
    
    self.picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(MAIN_WIDTH / 30, (MAIN_HEIGHT / 12 - MAIN_WIDTH / 6 / 5 * 3) / 2, MAIN_WIDTH / 6 / 5 * 3, MAIN_WIDTH / 6 / 5 * 3)];
    [_topView addSubview:_picImageView];
    _picImageView.backgroundColor = [UIColor whiteColor];
    
    [_picImageView setImage:[UIImage imageNamed:@"yiban.png"]];
    _picImageView.layer.borderWidth = 1;
    _picImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    _picImageView.layer.cornerRadius = (MAIN_WIDTH / 6 / 5 * 3)/ 2;
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(MAIN_WIDTH / 30 + MAIN_WIDTH / 6 / 5 * 3 + 5 / 414.0 * MAIN_WIDTH, MAIN_HEIGHT / 12 / 5, MAIN_WIDTH / 6, MAIN_HEIGHT / 12 / 5 * 3)];
    _titleLabel.text = @"一般";
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    _titleLabel.textColor = [UIColor whiteColor];
    [_topView addSubview:_titleLabel];
    _titleLabel.font = [UIFont systemFontOfSize:20 / 414.0 * MAIN_WIDTH];
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(MAIN_WIDTH / 2, 0, MAIN_WIDTH / 200 * 95, _topView.frame.size.height)];
    [_topView addSubview:_textField];
    //_textField.backgroundColor = [UIColor redColor];
    _textField.text = @"¥0.00";
    _textField.textColor = [UIColor whiteColor];
    _textField.textAlignment = NSTextAlignmentRight;
    _textField.delegate = self;
    _textField.font = [UIFont systemFontOfSize:25 / 414.0 * MAIN_WIDTH];
    [_textField becomeFirstResponder];
    _textField.tintColor = [UIColor clearColor];
    
#pragma mark --- 背景
    self.operateView = [[UIView alloc] initWithFrame:CGRectMake(0, MAIN_HEIGHT / 10, MAIN_WIDTH, MAIN_HEIGHT / 10 / 3 * 2)];
    _operateView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    
    _textField.inputAccessoryView = _operateView;
    
    self.dateButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _dateButton.frame = CGRectMake(0, 0, MAIN_WIDTH / 3, MAIN_HEIGHT / 30 * 2);
    
    _dateButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_operateView addSubview:_dateButton];
    _dateButton.tintColor = [UIColor lightGrayColor];
    _dateButton.titleLabel.font = [UIFont systemFontOfSize:20 / 667.0 * MAIN_HEIGHT];
    //解析时间戳
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a = [ dat timeIntervalSince1970] * 1000;
    CGFloat time = (CGFloat)a;
    NSString * timeStampString = [NSString stringWithFormat:@"%f", time];
    NSTimeInterval _interval=[timeStampString doubleValue] / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
    [objDateformat setDateFormat:@"yyyy年MM月dd日 HH:mm:ss.SSS"];
    
    NSString *mString = [objDateformat stringFromDate: date];
    NSString *str = [mString substringToIndex:11];
    NSString *string = [str substringFromIndex:5];
    [_dateButton setTitle:string forState:UIControlStateNormal];
    _dateButton.titleLabel.font = [UIFont systemFontOfSize:17];
    _dateButton.adjustsImageWhenHighlighted = NO;

    
    self.editButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _editButton.frame = CGRectMake(MAIN_WIDTH - [CalculateFrameView setWidth:60], [CalculateFrameView setHeight:4], [CalculateFrameView setWidth:40], [CalculateFrameView setHeight:40]);
//    [_editButton setTitle:@"备注" forState:UIControlStateNormal];
    [_editButton setImage:[UIImage imageNamed:@"NoRecord_32.png"] forState:UIControlStateNormal];
    _editButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_operateView addSubview:_editButton];
    [_editButton addTarget:self action:@selector(editButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    _editButton.tintColor = [UIColor lightGrayColor];
    _editButton.titleLabel.font = [UIFont systemFontOfSize:20 / 667.0 * MAIN_HEIGHT];
    
    self.keyboardView = [[UIView alloc] initWithFrame:CGRectMake(0, MAIN_HEIGHT / 2, MAIN_WIDTH, MAIN_HEIGHT / 3)];
    _keyboardView.backgroundColor = [UIColor whiteColor];
    
    _textField.inputView = _keyboardView;
    
#pragma mark -- 键盘
    UICollectionViewFlowLayout *keyboardLayout = [[UICollectionViewFlowLayout alloc] init];
    keyboardLayout.itemSize = CGSizeMake(MAIN_WIDTH / 5 * 4 / 3, _keyboardView.frame.size.height / 4);
    keyboardLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    keyboardLayout.minimumInteritemSpacing = 0;
    keyboardLayout.minimumLineSpacing = 0;
    
    
    self.keyboardCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, MAIN_WIDTH / 5 * 4, _keyboardView.frame.size.height) collectionViewLayout:keyboardLayout];
    [_keyboardView addSubview:_keyboardCollectionView];
    
    _keyboardCollectionView.delegate = self;
    _keyboardCollectionView.dataSource = self;
    
    [_keyboardCollectionView registerClass:[KeyboardCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([KeyboardCollectionViewCell class])];
    
    self.returnButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_returnButton setTitle:@"返回" forState:UIControlStateNormal];
    _returnButton.tintColor = [UIColor blackColor];
    _returnButton.frame = CGRectMake(MAIN_WIDTH / 5 * 4 + 1, 1, MAIN_WIDTH / 5 - 2, MAIN_HEIGHT / 6 - 2);
    _returnButton.titleLabel.font = [UIFont systemFontOfSize:17];
    [_keyboardView addSubview:_returnButton];
    [_returnButton addTarget:self action:@selector(returnButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    _returnButton.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    
    self.certainButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_certainButton setTitle:@"完成" forState:UIControlStateNormal];
    _certainButton.tintColor = [UIColor blackColor];
    _certainButton.frame = CGRectMake(MAIN_WIDTH / 5 * 4 + 1, MAIN_HEIGHT / 6 + 1, MAIN_WIDTH / 5 - 2, MAIN_HEIGHT / 6 - 2);
    _certainButton.titleLabel.font = [UIFont systemFontOfSize:17];
    [_keyboardView addSubview:_certainButton];
    [_certainButton addTarget:self action:@selector(certainButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    _certainButton.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    
#pragma mark -- 底部
        UICollectionViewFlowLayout *bottomLayout = [[UICollectionViewFlowLayout alloc] init];
    bottomLayout.itemSize = CGSizeMake(MAIN_WIDTH, MAIN_HEIGHT / 10 * 3);
    bottomLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    bottomLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    bottomLayout.minimumInteritemSpacing = 0;
    bottomLayout.minimumLineSpacing= 0;
    self.bottomCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, MAIN_HEIGHT / 10, MAIN_WIDTH, MAIN_HEIGHT / 10 * 3) collectionViewLayout:bottomLayout];
    [self addSubview:_bottomCollectionView];
    _bottomCollectionView.delegate = self;
    _bottomCollectionView.dataSource = self;
    [_bottomCollectionView registerClass:[BottomCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([BottomCollectionViewCell class])];
    _bottomCollectionView.backgroundColor = [UIColor whiteColor];
    _bottomCollectionView.pagingEnabled = YES;
    _bottomCollectionView.showsHorizontalScrollIndicator = NO;
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, MAIN_HEIGHT / 5 * 2 + MAIN_HEIGHT / 60, MAIN_WIDTH, MAIN_HEIGHT / 10 / 3 * 2)];
    [self addSubview:_page];
    
    _page.numberOfPages = 2;
    _page.currentPageIndicatorTintColor = [UIColor orangeColor];
    _page.pageIndicatorTintColor = [UIColor lightGrayColor];
    
    
    
    
    self.colorArray = @[@127, @184, @14,
                        @88, @94, @170,
                        @245, @130, @32,
                        @51, @71, @95,
                        @234, @102, @166,
                        @235, @79, @56,
                        @0, @154, @214,
                        @234, @128, @16,
                        @80, @183, @193,
                        @157, @85, @184,
                        @69, @185, @124,
                        @86, @171, @228,
                        @86, @69, @45,
                        @235, @79, @56,
                        @0, @187, @156,
                        @86, @171, @228,
                        @157, @85, @184,
                        @234, @128, @16, @252, @39, @28];
    self.picArray = [NSMutableArray arrayWithObjects:@"yiban.png", @"yongcan.png", @"jiaotong.png", @"fushi.png", @"liren.png", @"riyongpin.png", @"yule.png", @"shicai.png", @"lingshi.png", @"jiushui.png", @"zhufang.png", @"tongxun.png", @"jiaju.png", @"renqing.png", @"xuexi.png", @"yiliao.png", @"lvyou.png", @"shuma.png", @"qita.png", nil];
    self.titleArray = [NSMutableArray arrayWithObjects:@"一般", @"用餐", @"交通", @"服饰", @"丽人", @"日用品", @"娱乐", @"食材", @"零食", @"酒水", @"住房", @"通讯", @"家居", @"人情", @"学习", @"医疗", @"旅游", @"数码", @"其他", nil];
    
    self.inComePicArray = [NSMutableArray arrayWithObjects:@"yiban1.png", @"baoxiao.png", @"gongzi.png", @"hongbao.png", @"jianzhi.png", @"jiangjin.png", @"touzi.png", @"qitashouru.png", nil];
    self.incomeTitleArray = [NSMutableArray arrayWithObjects:@"一般", @"报销", @"工资", @"红包", @"兼职", @"奖金", @"投资", @"其他", nil];
    
    self.incomeColorArray = @[@234, @128, @16, @235, @79, @56, @235, @79, @56, @235, @79, @56, @244, @198, @0, @234, @128, @16, @235, @79, @56, @221, @101, @114];

    
    
    
    
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == _keyboardCollectionView) {
        return 12;
    }
    else {
        if (_isState == 1) {
            return 2;
        }
        else {
            return 1;
        }
        
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _bottomCollectionView) {
        BottomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([BottomCollectionViewCell class]) forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        cell.delegate = self;
        cell.isState = _isState;
        if (0 == indexPath.row) {
            cell.isBooL = 0;
        }
        else {
            cell.isBooL = 1;
        }
        return cell;
    }
    else {
        KeyboardCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([KeyboardCollectionViewCell class]) forIndexPath:indexPath];
        cell.titleLabel.text = [NSString stringWithFormat:@"%@", _keyboardArray[indexPath.row]];
        return cell;
    }
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _keyboardCollectionView) {
        if (_isReset == 0) {
            _str = @"".mutableCopy;
            _isReset = 1;
        }
        if (_textField.text.length < 11) {
            if (indexPath.row != 9 && indexPath.row != 11) {

                if (_isClick == 0) {
                    [_str appendString:_keyboardArray[indexPath.row]];
                    _textField.text = [NSString stringWithFormat:@"¥%@.00", _str];
                }
                else {
                    if ((_isClick != 2 && _isClick != 5)) {
                        [_str appendString:[NSString stringWithFormat:@".%@", _keyboardArray[indexPath.row]]];
                        _textField.text = [NSString stringWithFormat:@"¥%@0", _str];
                        _isClick = 2;
                    }
                    else if (_isClick == 2) {
                        _textField.text = [NSString stringWithFormat:@"¥%@%@", _str, _keyboardArray[indexPath.row]];
                        _isClick = 5;
                    }
                    else {
                        NSLog(@"不能再输入了");
                    }
                    
                }
                
            }
            else if (indexPath.row == 9) {
                _textField.text = @"¥0.00";
                _str = @"".mutableCopy;
                _isClick = 0;
                _isBooL = NO;
            }
            else {
                if (_isBooL == NO) {
                    self.isClick = 1;
                    _isBooL = YES;
                }
                else {
                    NSLog(@"不能再输入了");
                }
                
            }
        }
        else {
            
            if (self.isClick == 4) {
                [_str appendString:_keyboardArray[indexPath.row]];
                _textField.text = [NSString stringWithFormat:@"¥%@", _str];
                self.isClick = 5;
            }

            
            if (self.isClick == 3) {
                if (indexPath.row != 11 && indexPath.row != 9) {
                    [_str appendString:[NSString stringWithFormat:@".%@", _keyboardArray[indexPath.row]]];
                    _textField.text = [NSString stringWithFormat:@"¥%@0", _str];
                    self.isClick = 4;
                }
                
                
            }
            
            if (indexPath.row == 9) {
                _textField.text = @"¥0.00";
                _str = @"".mutableCopy;
                _isClick = 0;
                _isBooL = NO;
            }
            else if (indexPath.row == 11){
                if (_isBooL == NO) {
                    self.isClick = 3;
                    _isBooL = YES;
                }
                else {
                    NSLog(@"不能在输入了");
                }
            }
            else {
                if (self.isClick == 5) {
                    NSLog(@"不能输入了");
                }
                else {
                    NSLog(@"只能输入小数点后的钱数了");
                }
                
            }
            
            
            

        }
    }
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _bottomCollectionView) {
        CGFloat x = _bottomCollectionView.contentOffset.x / MAIN_WIDTH;
        _page.currentPage = (NSInteger)x;
    }
}
- (void)sendPic:(NSString *)pic title:(NSString *)title array:(NSArray *)array frame:(CGRect)frame{
    self.pic = pic;
    self.describe = title;
    
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(frame.origin.x, frame.origin.y + MAIN_HEIGHT / 12, MAIN_WIDTH / 6 / 5 * 3, MAIN_WIDTH / 6 / 5 * 3)];
    [self addSubview:image];
    image.image = [UIImage imageNamed:pic];
    image.backgroundColor = [UIColor whiteColor];
    image.layer.cornerRadius = MAIN_WIDTH / 6 / 5 * 3 / 2;
    image.layer.masksToBounds = YES;
    [UIView animateWithDuration:0.5 animations:^{
        
        image.frame = CGRectMake(MAIN_WIDTH / 30, 11, _topView.frame.size.height - 22, _topView.frame.size.height - 22);
    } completion:^(BOOL finished) {
        UIColor *bgColor = [UIColor colorWithRed:[array[0] floatValue] / 255 green:[array[1] floatValue] / 255.0 blue:[array[2] floatValue] / 255.0 alpha:1];
        [self.topView mdInflateAnimatedFromPoint:CGPointMake(0, _topView.frame.size.height / 2)  backgroundColor:bgColor duration:AnimationChangeColorDuration completion:nil];
        [_picImageView setImage:[UIImage imageNamed:pic]];
        _picImageView.backgroundColor = [UIColor whiteColor];
        _titleLabel.text = title;
        [image removeFromSuperview];
    }];
    
    
}


- (void)returnButtonAction:(UIButton *)button {
[self.delegate popViewActionPic:nil describe:nil price:nil];
}

- (void)certainButtonAction:(UIButton *)button {
    NSString *str = [NSString stringWithString:_textField.text];
    NSString *string = [ str substringFromIndex:1];

    if (!(0 == [string intValue])) {
        if (self.index == 1) {
            
            if (_isState == 1) {
                _moneyStr = string;
                [self.delegate popViewActionPic:self.pic describe:self.titleLabel.text price:self.moneyStr];
                
            }
            else {
                _moneyStr = string;
                [self.delegate popViewActionPic:self.pic describe:self.titleLabel.text price:self.moneyStr];
            }
        }else {
            if (self.pic == nil) {
                if (_isState == 1) {
                    self.pic = @"yiban.png";
                    
                }
                else {
                    self.pic = @"yiban1.png";
                }
                self.describe = @"一般";
                
                [self.delegate popViewActionPic:self.pic describe:self.describe price:string];
            }
            else {
                if (_describe == nil) {
                    self.describe = @"一般";
                }
                [self.delegate popViewActionPic:self.pic describe:self.describe price:string];
            }
        }
    }
    else {
        UIAlertView *alert  = [[UIAlertView alloc] initWithTitle:@"请输入有效金额!" message:nil delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [alert show];
    }

}

- (void)editButtonAction:(UIButton *)button {
    [self.delegate jumpToEditView];
}

- (void)setStr:(NSMutableString *)str {
    _str = str;
    if ([_str isEqualToString:@""]) {
        self.textField.text = [NSString stringWithFormat:@"¥0.00"];
    }
    else {
        self.textField.text = [NSString stringWithFormat:@"¥%@", _str];
        _str = @"".mutableCopy;
    }
}
- (void)setMoneyStr:(NSString *)moneyStr {
    _moneyStr = moneyStr;
    _textField.text =  [NSString stringWithFormat:@"¥%@", _moneyStr];
}
- (void)setType:(NSString *)type {
    if (_type != type) {
        _type = type;
    }
}

- (void)setPicName:(NSString *)picName {

    self.isReset = 0;
    if (_picName != picName) {
        _picName = picName;
        _pic = picName;
        self.picImageView.image = [UIImage imageNamed:_picName];
        if ([self.type isEqualToString:@"2"]) {
            for (NSInteger i = 0; i < self.picArray.count; i++) {
                if ([_picName isEqualToString:self.picArray[i]]) {
                    self.index = i;
                    UIColor *bgColor = [UIColor colorWithRed:[self.colorArray[3 * i] floatValue] /255.0 green:[self.colorArray[3 * i + 1] floatValue] /255.0 blue:[self.colorArray[3 * i + 2] floatValue] /255.0 alpha:1];
                    _topView.backgroundColor = bgColor;
                    self.titleLabel.text = [NSString stringWithFormat:@"%@", self.titleArray[i]];
                    self.index = 1;
                    
                    break;
                }
            }
        }
        else {
            for (NSInteger i = 0; i < self.inComePicArray.count; i++) {
                if ([_picName isEqualToString:self.inComePicArray[i]]) {
                    self.index = i;
                    UIColor *bgColor = [UIColor colorWithRed:[self.incomeColorArray[3 * i] floatValue] /255.0 green:[self.incomeColorArray[3 * i + 1] floatValue] /255.0 blue:[self.incomeColorArray[3 * i + 2] floatValue] /255.0 alpha:1];
                    _topView.backgroundColor = bgColor;
                    self.titleLabel.text = [NSString stringWithFormat:@"%@", self.incomeTitleArray[i]];
                    self.index = 1;
                    
                    break;
                }
            }
        }
    }
    else {
        _picName = self.pic;
        self.index = 1;

    }
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
